package com.amoby.main.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.amoby.base.common.Pager;
import com.amoby.main.mapper.AlipayOrderMapper;
import com.amoby.main.service.AlipayOrderService;
import com.amoby.main.service.AlipayService;

/**
 * Title：AlipayOrderServiceImpl
 * Desc：订餐订单实现
 * @address http://www.yingmob.com/
 * @author zhaoyy
 * @date 2015-10-10 上午9:52:15
 */
@Service
public class AlipayOrderServiceImpl implements AlipayOrderService {
    private static Log          logger = LogFactory.getLog(AlipayOrderServiceImpl.class);
    @Autowired
    private AlipayOrderMapper   alipayOrderMapper;
    @Autowired
    private AlipayService       alipayService;

    @Override
    public Pager query(Pager pager) {
        pager.setResult(alipayOrderMapper.selectByPager(pager));
        return pager;
    }

    @Override
    public List<Map<String, Object>> queryBsbAccByOrderno(Map<Object, Integer> inputMap) {
        return alipayOrderMapper.queryBsbAccByOrderno(inputMap);
    }

    @Override
    public Map<String, Object> refundOrder(String orderNum) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Map<String, Object> map = alipayService.alipayorderRefund(orderNum);
            if ("success".equals(map.get("r").toString())) {// 支付宝活动退款成功
                result.put("r", true);
                result.put("m", "退款成功");
            } else {
                result.put("r", false);
                result.put("m", "退款失败");
            }
        } catch (Exception e) {
            logger.error("支付宝活动订单退款异常=" + orderNum, e);
            result.put("r", false);
            result.put("m", "退款异常");
            throw new IllegalArgumentException("支付宝活动订单退款异常=" + orderNum);
        }
        return result;
    }
   
    /** 申请退款 */
    @Override
    public Map<String, Object> sqrefundOrder(String orderNum) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            Map<String, Object> map = alipayService.alipayOrderSqRefund(orderNum);
            if ("success".equals(map.get("r").toString())) {// 支付宝活动申请退款成功
                result.put("r", true);
                result.put("m", map.get("m"));
            } else {
                result.put("r", false);
                result.put("m", map.get("m"));
            }
        } catch (Exception e) {
            logger.error("支付宝活动订单申请退款异常=" + orderNum, e);
            result.put("r", false);
            result.put("m", "退款异常");
            throw new IllegalArgumentException("支付宝活动订单申请退款异常=" + orderNum);
        }
        return result;
    }
    
    @Override
    public List<Map<String, Object>> exportAll(Map<String, Object> map) {
        List<Map<String, Object>> list = alipayOrderMapper.exportAll(map);
        return list;
    }

}
